package top.lyc829.mymusic.ui.fragment.play_list_ground

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import top.lyc829.mymusic.R
import top.lyc829.mymusic.base.BaseFragment
import top.lyc829.mymusic.databinding.FragmentPlayListGroundBinding
import top.lyc829.mymusic.model.bean.PlayListSimple

class PlayListGroundFragment :
    BaseFragment<PlayListGroundViewModel, FragmentPlayListGroundBinding>() {

    companion object {
        const val TAG = "PlayListGroundFragment"
    }

    override fun getViewModel() = ViewModelProvider(this).get(PlayListGroundViewModel::class.java)
    override fun getBinding(inflater: LayoutInflater) = FragmentPlayListGroundBinding.inflate(inflater)

    override fun initView(savedInstanceState: Bundle?) {
        binding.toolbar.toolbar.title = "歌单广场"
        binding.toolbar.toolbar.setNavigationOnClickListener {
            findNavController().popBackStack()
        }

        binding.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(context))
            setRefreshFooter(ClassicsFooter(context))
            setOnRefreshListener {
                mViewModel.pageInfo.reset()
                mViewModel.getPlayList()
            }
            setOnLoadMoreListener {
                mViewModel.getPlayList()
            }
        }

        val manager = GridLayoutManager(context, 3)
        manager.orientation = GridLayoutManager.VERTICAL
        binding.recyclerView.layoutManager = manager
        binding.recyclerView.adapter = mViewModel.adapter
    }

    override fun initClickListener() {
        mViewModel.adapter.setOnItemClickListener { _, _, position ->
            val id: Long = mViewModel.adapter.getItem(position).id
            val copywriter = mViewModel.adapter.getItem(position).copywriter
            val bundle = Bundle()
            bundle.putLong("id", id)
            bundle.putString("copywriter", copywriter)
            findNavController().navigate(R.id.action_playListGroundFragment_to_playListDetailFragment, bundle)
        }
    }

    override fun createObserver() {
        mViewModel.playList.observe(viewLifecycleOwner) {
            Log.d(TAG, mViewModel.pageInfo.page.toString())
            if (mViewModel.pageInfo.isFirstPage()) {
                mViewModel.adapter.setList(it)
            } else {
                mViewModel.adapter.addData(it)
            }
        }

        mViewModel.isLoading.observe(viewLifecycleOwner) {
            if (!it) {
                binding.refreshLayout.finishRefresh()
                binding.refreshLayout.finishLoadMore()
            }
        }
        mViewModel.errorLiveData.observe(this) {
            ToastUtils.showShort(it.errorMsg)
        }
    }

    override fun initData() {
        if (mViewModel.pageInfo.isFirstPage()) {
            binding.refreshLayout.autoRefresh()
        }
    }
}